< previous page page_91 next page >

Page 91
during the program. For now, you'll explore the Action parameter. The next set of code adds a routine to the Validate step that pops up a message box each time the arrow buttons of a data control are clicked.
Just like the Options property constants, Visual Basic also provides a set of predefined constants for all the possible Action values reported in the Validate event. Although these constants are handy, they are not very useful to users of your programs. The following code example shows you how to translate those constants into a friendly message using a string array. Add the following line to the general declarations section of the form.
Option Explicit
Dim VldMsg(4) As String ' declare message array
Now add the following procedure, which loads a set of messages into the array you declared previously. These messages are displayed each time the corresponding action occurs in the Validate event. Notice that you are using the predefined Visual Basic constants.
Public Sub MakeVldMsgArray()
    VldMsg(vbDataActionMoveFirst) = "MoveFirst"
    VldMsg(vbDataActionMovePrevious) = "MovePrevious"
    VldMsg(vbDataActionMoveNext) = "MoveNext"
    VldMsg(vbDataActionMoveLast) = "MoveLast"
End Sub
Update the Form_Load event to call the MakeVldMsgArray procedure. You can see that MakeVldMsgArray has been added at the start of the event. Here's the code:
Private Sub Form_Load()
   MakeVldMsgArray ' create message array
   OpenDB ' open the database, set dynaset
   BindControls ' link controls to data fields
End Sub
Now you need to add the one bit of code that will be executed each time the Validate event occurs. This code displays a simple message each time you click the arrow buttons of the data control. The actual message is determined by the Action value that Visual Basic passes to the Validate event. The Action value is, of course, determined by the arrow button on the data control that you click while the program is running.
Now you have to replace the "Validating data" message that you entered in the previous example. Here's the new code:
Private Sub Data1_Validate(Action As Integer, Save As Integer)
    MsgBox VldMsg(Action) ' message based on user action
End Sub
Save and run the program to see a message box that tells you what you probably already know! There are several other actions that can occur during the Validate event. You'll explore these actions on Day 5.

 
< previous page page_91 next page >

If you like this book, buy it!